package com.yumao.recyclerview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.animators.ScaleInRightAnimator;

/**
 * recyclerView 缓存的是Viewholder 机制和listView是一样的
 * setLayoutManager 管理布局   addItemDecoration 设置分割样式     setItemAnimator设置动画
 *
 * getItemViewType 可实现多种样式的RecyclerView
 */
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<String> list;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        initData();
        adapter = new MyAdapter(this, list);
        adapter.setRecyclerClickListener(new MyAdapter.RecyclerClickListener() {
            @Override
            public void onClick(View view, int position) {
                T.showToast("click" + position);
            }

            @Override
            public void onLongClick(View view, int position) {
                T.showToast("longClick" + position);
            }
        });
        recyclerView.setAdapter(adapter);
        //设置RecylerView 布局管理
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//        recyclerView.setLayoutManager(new GridLayoutManager(this,3,GridLayoutManager.HORIZONTAL,false));
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.HORIZONTAL));
        //设置recylerView 分割线
//        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        recyclerView.addItemDecoration(new SpacesItemDecoration(10));
        //设置recyclerView 动画
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setItemAnimator(new ScaleInRightAnimator());
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 'a'; i < 'z'; i++) {
            list.add("" + (char) i);
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                adapter.add();
                break;
            case R.id.button2:
                adapter.delete();
                break;
            case R.id.button3:
                Intent intent = new Intent(this, Main2Activity.class);
                startActivity(intent);
                break;
            case R.id.button4:
                Intent intent1 = new Intent(this, Main3Activity.class);
                startActivity(intent1);
                break;
        }
    }
}
